﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.ServiceModel;
using WJP.Configuration;
using WJP.DataPortal.WCF;
using WJP.Logging;
using WJP.Reflection;

namespace WJP.DataPortal
{
    /// <summary>
    /// 应用服务器控制器
    /// </summary>
    public class HostController : DomainController
    {
        /// <summary>
        /// 应用服务器时间
        /// </summary>
        /// <returns></returns>
        public virtual DateTime GetHostTime()
        {
            return DateTime.Now;
        }

        /// <summary>
        /// 应用服务器IP
        /// </summary>
        /// <returns></returns>
        public virtual string GetHostIP()
        {
            try
            {
                foreach (IPAddress hostAddress in Dns.GetHostAddresses(Dns.GetHostName()))
                {
                    if (hostAddress.AddressFamily == AddressFamily.InterNetwork)
                        return hostAddress.ToString();
                }
                return "";
            }
            catch (Exception ex)
            {
                throw new SysException("获取本机IP出错", ex);
            }
        }

        /// <summary>
        /// 获取应用服务器信息
        /// </summary>
        /// <returns></returns>
        [IgnoreProxy]
        public IList<HostInfo> GetHostInfo()
        {
            if (PlatformEnvironment.Location.DataPortalMode == DataPortalMode.Unit)
                return HostManager.Default.Hosts;

            var config = PlatformEnvironment.Configuration;
            var channelFactory = new ChannelFactory<IHost>(config.Client.HostInfoEndpoint);
            List<HostInfo> hostInfoList = new List<HostInfo>();
            foreach (AddressElement host in config.Client.HostCollection)
            {
                try
                {
                    hostInfoList.AddRange(channelFactory.CreateChannel(new EndpointAddress(host.BaseAddress + config.Client.HostInfoAddress)).GetHostInfos());
                }
                catch (Exception ex)
                {
                    LogManager.Logger.Error("获取Host信息失败", ex);
                }
            }

            return hostInfoList;
        }

        /// <summary>
        /// 停止应用服务器
        /// </summary>
        /// <param name="address"></param>
        [IgnoreProxy]
        public void StopHost(string address)
        {
            if (PlatformEnvironment.Location.DataPortalMode == DataPortalMode.CSServer)
            {
                PlatformConfiguration configuration = PlatformEnvironment.Configuration;
                ChannelFactory<IHost> channelFactory = new ChannelFactory<IHost>(configuration.Client.HostInfoEndpoint);
                foreach (AddressElement host in configuration.Client.HostCollection)
                    channelFactory.CreateChannel(new EndpointAddress(host.BaseAddress + configuration.Client.HostInfoAddress)).Stop(address);
            }
            else
                HostManager.Default.Stop(address);
        }

        /// <summary>
        /// 启动应用服务器
        /// </summary>
        /// <param name="address"></param>
        [IgnoreProxy]
        public void StartHost(string address)
        {
            if (PlatformEnvironment.Location.DataPortalMode == DataPortalMode.CSServer)
            {
                PlatformConfiguration configuration = PlatformEnvironment.Configuration;
                ChannelFactory<IHost> channelFactory = new ChannelFactory<IHost>(configuration.Client.HostInfoEndpoint);
                foreach (AddressElement host in configuration.Client.HostCollection)
                    channelFactory.CreateChannel(new EndpointAddress(host.BaseAddress + configuration.Client.HostInfoAddress)).Start(address);
            }
            else
                HostManager.Default.Start(address);
        }
    }
}
